﻿using System;
using System.Collections;
using System.Text;
using System.Data;
using GeneralLibs;
using System.Net.Mail;
using System.Data.SqlClient;

namespace GeneralLibs.Lists
{
    public class Cho : DoiTuong
    {
        #region string MaCho property
        public string MaCho
        {
            get
            {
                if (_DataRow["MaCho"] is string) return (string)_DataRow["MaCho"];
                return "";
            }
            set
            {
                _DataRow["MaCho"] = value;
            }
        }
        #endregion

        #region string TenCho property
        public string TenCho
        {
            get
            {
                if (_DataRow["TenCho"] is string) return (string)_DataRow["TenCho"];
                return "";
            }
            set
            {
                _DataRow["TenCho"] = value;
            }
        }
        #endregion

        #region int HangCho property
        public int HangCho
        {
            get
            {
                if (_DataRow["HangCho"] is int) return (int)_DataRow["HangCho"];
                return -1;
            }
            set
            {
                _DataRow["HangCho"] = value;
            }
        }
        #endregion

        #region Guid DiaBanID property
        public Guid DiaBanID
        {
            get
            {
                if (_DataRow["DiaBanID"] is Guid) return (Guid)_DataRow["DiaBanID"];
                return Guid.Empty;
            }
            set
            {
                _DataRow["DiaBanID"] = value;
            }
        }
        #endregion

        public Cho(DataRow dataRow)
            : base(dataRow)
        {
        }
    }

    public class DanhMucCho : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucCho _Instance;
        public static DanhMucCho Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucCho();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                    _Instance.DataView.Sort = "MaCho";
                }
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "Cho";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new Cho((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public Cho this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new Cho(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public Cho NewDoiTuong()
        {
            try
            {
                return new Cho(Instance.DataTable.NewRow());
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public Cho GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new Cho(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public Cho GetByID(Guid objID)
        {
            try
            {
                return GetByProperty("ID", objID);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static DanhMucCho Filter(string filterExpression)
        {
            try
            {
                DanhMucCho obj = new DanhMucCho();
                obj.DataView = new DataView(DanhMucCho.Instance.DataTable, filterExpression, "", DataViewRowState.CurrentRows);
                return obj;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static DanhMucCho GetAll()
        {
            try
            {
                return Instance;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
    }
}